package com.qf.smartplatform.controller;

import com.qf.smartplatform.dto.R;
import com.qf.smartplatform.pojo.SysDevice;
import com.qf.smartplatform.service.DeviceService;
import com.qf.smartplatform.valigroup.delete;
import com.qf.smartplatform.valigroup.insert;
import com.qf.smartplatform.valigroup.select;
import com.qf.smartplatform.valigroup.update;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/*** @author Tgb
 * @version V1.0
 * @Project smartplatform
 * @Package com.qf.smartplatform.controller
 * @Description:
 * @Date 2022/9/9 15:57
 */
@RestController
@RequestMapping("device")
public class DeviceController {
    private DeviceService deviceService;
    @Autowired
    public void setDeviceService(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    @PostMapping("/add")
    public R addDevice(@Validated(insert.class) @RequestBody SysDevice sysDevice){
        System.out.println(sysDevice);
    deviceService.addDevice(sysDevice);

    return R.OK();
    }

    @PostMapping("/delete")
    public R deleteDevice(@Validated(delete.class) @Param("deviceId") String deviceId){
        deviceService.deleteById(deviceId);
        return R.OK();
    }
    @PostMapping("/update")
    public R updateDevice(@Validated(update.class) @RequestBody SysDevice sysDevice){
        deviceService.update(sysDevice);
        return R.OK();
    }

    @PostMapping("/select")
    public R findDevice(@Validated(select.class) @Param("deviceId") String deviceId){
        SysDevice sysDevice=deviceService.findById(deviceId);
        return R.OK(sysDevice);
    }
    @PostMapping("/selectAll")
    public R findAll(){
       return R.OK(deviceService.findAll()) ;
    }

    @PostMapping("/command")
    public R sendCommand(@Param("deviceId") String deviceId,@Param("command")String command){
        deviceService.sendCommand(deviceId,command);
        return R.OK();
    }
}
